package com.xhj.controller;

import com.alibaba.fastjson.JSONObject;
import com.xhj.service.PermissionService;
import com.xhj.service.UserService;
import com.xhj.util.ResultEntity;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/acl/index/")
//@CrossOrigin
@Slf4j
public class IndexController {

    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "获取用户信息")
    @GetMapping("info")
    public ResultEntity getInfo(){
        // 通过securityContext获取当前登录用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        Map<String, Object> userInfo = userService.getUserInfo(username);
        return ResultEntity.successWithData().data(userInfo);
    }

    @ApiOperation(value = "获取菜单")
    @GetMapping("menu")
    public ResultEntity getMenu(){
        // 通过securityContext获取当前登录用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        List<JSONObject> permissionList = permissionService.getMenuByUserName(username);

        return ResultEntity.successWithData().data("permissionList", permissionList);
    }

    @ApiOperation(value = "注销")
    @RequestMapping("logout")
    public ResultEntity logout(){
        return ResultEntity.successWithoutData();
    }
}
